/**
 * 
 */
package lt.vin.exaatto.client.ui;

import java.util.List;

import lt.vin.exaatto.client.events.UserInfoAvailableEvent;
import lt.vin.exaatto.client.events.UserInfoAvailableHandler;
import lt.vin.exaatto.shared.Chat;
import lt.vin.exaatto.shared.UserInformation;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Vincentas
 * 
 */
public class ChatManagerUI extends Composite implements UserInfoAvailableHandler {

	private static ChatManagerUIUiBinder uiBinder = GWT.create(ChatManagerUIUiBinder.class);

	interface ChatManagerUIUiBinder extends UiBinder<Widget, ChatManagerUI> {
	}

	DialogBox loadDialog = new DialogBox(false, true);

	@UiField
	Panel panel;

	@UiField
	ChatPanel chatPanel;

	@UiField
	OwnerPanel ownerPanel;

	@UiField
	ManagersPanel managersPanel;

	@UiField
	MemberPanel membersPanel;

	@UiField
	HorizontalPanel goHere;

	public ChatManagerUI() {
		initWidget(uiBinder.createAndBindUi(this));

		Stat.getHandlerManager().addHandler(UserInfoAvailableHandler.type, this);

		// AuthSubStatus status =
		// User.getStatus("http://www.google.com/m8/feeds/");
		// if (status == AuthSubStatus.LOGGED_OUT) {
		// goHere.add(new Button("Login", new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// User.login("http://www.google.com/m8/feeds/");
		// }
		//
		// }));
		// } else {
		// final MultiWordSuggestOracle suggestOracle = new
		// MultiWordSuggestOracle();
		// goHere.add(new SuggestBox(suggestOracle));
		//
		// ContactQuery query =
		// ContactQuery.newInstance("http://www.google.com/m8/feeds/contacts/default/full");
		// query.setMaxResults(500);
		// Stat.getContactsService().getContactFeed(query, new
		// ContactFeedCallback() {
		//
		// @Override
		// public void onSuccess(ContactFeed result) {
		// for (ContactEntry entry : result.getEntries()) {
		// Text title = entry.getTitle();
		//
		// for (Email email : entry.getEmailAddresses()) {
		// suggestOracle.add(title.getText() + " <" + email.getAddress() + ">");
		// System.out.println(email.getLabel());
		// System.out.println(email.getDisplayName());
		// System.out.println(email.getRel());
		// }
		// }
		// }
		//
		// @Override
		// public void onFailure(CallErrorException caught) {
		// System.out.println(caught.toString());
		// caught.printStackTrace();
		// }
		// });
		// }

		getUserInformation();
	}

	private void getUserInformation() {
		loadDialog.setWidget(new LoadPanel());
		loadDialog.center();

		Stat.getRpcService().getUserInformation(new AsyncCallback<UserInformation>() {

			@Override
			public void onSuccess(UserInformation userInformation) {
				Stat.getHandlerManager().fireEvent(new UserInfoAvailableEvent(userInformation));
			}

			@Override
			public void onFailure(Throwable caught) {
				final int retryAfter = 10;
				final RetryPanel retryPanel = new RetryPanel();
				retryPanel.setTimeLeft(retryAfter);

				loadDialog.setWidget(retryPanel);
				Timer timer = new Timer() {
					int secondsLeft = retryAfter;

					@Override
					public void run() {
						secondsLeft--;
						retryPanel.setTimeLeft(secondsLeft);

						if (secondsLeft <= 0) {
							cancel();
							getUserInformation();
						}
					}
				};

				timer.scheduleRepeating(1000);
			}
		});
	}

	@Override
	public void userInfoAvailable(UserInformation userInfo) {
		if (userInfo.isLoggedIn()) {
			Stat.setUser(userInfo.getUsername());
			loadDialog.hide();

			panel.setVisible(true);
			List<Chat> chats = userInfo.getChats();
			chatPanel.setElements(chats);
		} else {
			loadDialog.setWidget(new LoginPanel(userInfo.getLoginUrl()));
		}
	}
}
